package com.source3g.dubbo.config.boot.health;

import com.alibaba.dubbo.rpc.service.EchoService;
import com.source3g.dubbo.config.boot.DubboAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.CompositeHealthIndicatorConfiguration;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.Map;

/**
 * Created by jianglin on 2017/4/1
 */
@ConditionalOnClass({EchoService.class})
@AutoConfigureOrder
@Configuration
@AutoConfigureAfter(DubboAutoConfiguration.class)
@ConditionalOnProperty(prefix = "spring.dubbo", name = "health", havingValue = "true")
public class DubboServiceHealthIndicatorConfiguration extends CompositeHealthIndicatorConfiguration<DubboServiceHealthIndicator,
        EchoService> {

    @Autowired(required = false)
    @Lazy
    private Map<String, EchoService> echoServiceMap;

    @Bean
    @ConditionalOnMissingBean(name = "dubboHealthIndicator")
    public HealthIndicator dubboHealthIndicator() {
        return createHealthIndicator(this.echoServiceMap);
    }

    @Override
    protected DubboServiceHealthIndicator createHealthIndicator(EchoService source) {
        return new DubboServiceHealthIndicator(source);
    }

}
